﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace Emmajn.ChildParentExtension.SilverlightClassLibrary
{
    /// <summary>
    /// Represents an individual's name.
    /// </summary>
    public class Name
    {
        /// <summary>
        /// Represent the largest possible length of the FirstName string. This name is constant.
        /// </summary>
        public const int MaxFirstNameLength = 15;
        /// <summary>
        /// Represent the largest possible length of the LastName string. This name is constant.
        /// </summary>
        public const int MaxLastNameLength = 15;
        /// <summary>
        /// Represent the largest possible length of the Suffix string. This name is constant.
        /// </summary>
        public const int MaxSuffixLength = 5;

        private string firstName;
        private char? middleInitial;
        private string lastName;
        private string suffix;

        /// <summary>
        /// Initializes a new instance of the name class.
        /// </summary>
        public Name() { }

        /// <summary>
        /// Initializes a new instance of the name class.
        /// </summary>
        /// <param name="firstName">First name (up to 15 characters)</param>
        /// <param name="middleInitial">[Middle initial (1 character)]</param>
        /// <param name="lastName">Last name (up to 15 characters)</param>
        /// <param name="suffix">[Suffix (up to 5 characters)]</param>
        public Name(string firstName, char? middleInitial, string lastName, string suffix = null)
        {
            FirstName = firstName;
            MiddleInitial = middleInitial;
            LastName = lastName;
            Suffix = suffix;
        }

        /// <summary>
        /// First name (up to 15 characters)
        /// </summary>
        [Required]
        public string FirstName
        {
            get
            {
                return firstName;
            }
            set
            {
                Assert.IsValidRequiredString(value, MaxFirstNameLength);
                firstName = value;
            }
        }

        /// <summary>
        /// [Middle initial (1 character)]
        /// </summary>
        public char? MiddleInitial
        {
            get
            {
                return middleInitial;
            }
            set
            {
                middleInitial = value;
            }
        }

        /// <summary>
        /// Last name (up to 15 characters)
        /// </summary>
        [Required]
        [StringLength(15)]
        [Display(Name="First name")]
        public string LastName
        {
            get
            {
                return this.lastName;
            }
            set
            {
                //Assert.IsValidRequiredString(value, MaxLastNameLength);
                
                lastName = value;
            }
        }

        /// <summary>
        /// [Suffix (up to 5 characters)]
        /// </summary>
        public string Suffix
        {
            get
            {
                return suffix;
            }
            set
            {
                Assert.IsValidOptionalString(value, MaxSuffixLength);
                suffix = value;
            }
        }

        /// <summary>
        /// Returns a string that represents the current object.
        /// </summary>
        public override string ToString()
        {
            if (middleInitial.HasValue)
                if (suffix == null)
                    return string.Format("{0} {1}. {2}", firstName, middleInitial, lastName);
                else
                    return string.Format("{0} {1}. {2}, {3}", firstName, middleInitial, lastName, suffix);
            else
                if (suffix == null)
                    return string.Format("{0} {1}", firstName, lastName);
                else
                    return string.Format("{0} {1}, {2}", firstName, lastName, suffix);
        }
    }
}
